package com.rainkaze.lifeharbor.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView; // 仍然需要导入 ImageView，如果直接访问其属性
import android.widget.TextView;  // 同上
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.rainkaze.lifeharbor.R;
import com.rainkaze.lifeharbor.databinding.ItemToolBinding; // 导入 ViewHolder 的 ViewBinding 类
import com.rainkaze.lifeharbor.db.DatabaseHelper;
import com.rainkaze.lifeharbor.model.ToolItem;

import java.util.List;

public class ToolAdapter extends RecyclerView.Adapter<ToolAdapter.ToolViewHolder> {

    private List<ToolItem> toolItems;
    private OnToolClickListener listener;
    private Context context;
    private DatabaseHelper dbHelper;
    private int uniformIconColor;

    public interface OnToolClickListener {
        void onToolClick(ToolItem toolItem, int position);
    }

    public ToolAdapter(Context context, List<ToolItem> toolItems, OnToolClickListener listener) {
        this.context = context;
        this.toolItems = toolItems;
        this.listener = listener;
        this.dbHelper = new DatabaseHelper(context);
        this.uniformIconColor = ContextCompat.getColor(context, R.color.uniform_icon_color);
    }

    @NonNull
    @Override
    public ToolViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemToolBinding binding = ItemToolBinding.inflate(LayoutInflater.from(context), parent, false);
        return new ToolViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ToolViewHolder holder, int position) {
        ToolItem currentItem = toolItems.get(position);

        holder.binding.toolName.setText(currentItem.getName());

        if (currentItem.getIconResId() != 0) {
            holder.binding.toolIcon.setImageResource(currentItem.getIconResId());
        } else {
            holder.binding.toolIcon.setImageResource(R.drawable.all);
        }

        // 根据收藏状态设置toolIcon的颜色
        if (currentItem.isFavorite()) {
            // 收藏的设置为金色
            holder.binding.toolIcon.setColorFilter(ContextCompat.getColor(context, R.color.gold_color), PorterDuff.Mode.SRC_IN);
        } else {
            // 没收藏的设置为灰色
            holder.binding.toolIcon.setColorFilter(uniformIconColor, PorterDuff.Mode.SRC_IN);
        }

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onToolClick(currentItem, holder.getAdapterPosition());
            }
        });

        holder.itemView.setOnLongClickListener(v -> {
            showFavoriteConfirmationDialog(currentItem, holder.getAdapterPosition());
            return true;
        });
    }

    private void showFavoriteConfirmationDialog(ToolItem item, int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        final boolean currentIsFavorite = item.isFavorite();
        String dialogMessage = currentIsFavorite ?
                "要取消收藏 \"" + item.getName() + "\" 吗？" :
                "要收藏 \"" + item.getName() + "\" 吗？";
        String positiveButtonText = currentIsFavorite ? "取消收藏" : "收藏";

        builder.setMessage(dialogMessage)
                .setPositiveButton(positiveButtonText, (dialog, id) -> {
                    boolean newFavoriteStatus = !currentIsFavorite;
                    item.setFavorite(newFavoriteStatus);
                    dbHelper.setToolFavoriteStatus(item.getToolIdentifier(), newFavoriteStatus);
                    notifyItemChanged(position);
                    Toast.makeText(context,
                            item.getName() + (newFavoriteStatus ? " 已收藏" : " 已取消收藏"),
                            Toast.LENGTH_SHORT).show();
                })
                .setNegativeButton("不了", (dialog, id) -> dialog.dismiss());
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    @Override
    public int getItemCount() {
        return toolItems == null ? 0 : toolItems.size();
    }

    public void updateToolItems(List<ToolItem> newToolItems) {
        this.toolItems.clear();
        this.toolItems.addAll(newToolItems);
        notifyDataSetChanged();
    }

    static class ToolViewHolder extends RecyclerView.ViewHolder {
        ItemToolBinding binding;

        public ToolViewHolder(ItemToolBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}